<template>
    <div>
        <van-nav-bar title="发布帖子" left-text="主页" left-arrow @click-left="$router.push('/home/home')" fixed />

        <div class="block"></div>
        <van-form @failed="onFailed" @submit="onSubmit(enterData)">
            <van-cell-group inset>
                <!-- 通过 pattern 进行正则校验 -->
                <van-field v-model="enterData.title" name="title" placeholder="标题" />
                <van-field v-model="enterData.content" name="content" placeholder="内容...." />
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    提交
                </van-button>
            </div>
        </van-form>

    </div>
</template>

<script setup>
// 导入依赖
import { POST, GET } from "../../tools/fetch.js";
import { postApi } from '../../tools/api.js';
import { useRouter } from 'vue-router';
const router = useRouter();
// token
import { useUserStore } from '../../tools/storage.js';
// 初始化
const userStore = useUserStore();
import { ref } from 'vue';
import { showSuccessToast } from 'vant';

//存储
const enterData = ref({
    title: '',
    content: ''
})


// 提交表单并更新数据库
const onSubmit = async (values) => {
    console.log('这是values', values)
    const resResult = await POST({
        api: postApi.creatPost,
        data: {
            title: values.title,
            content: values.content,
        }
    })
    console.log(resResult);
    showSuccessToast('发表成功')
    setTimeout(() => {
        router.push('/home/home')
    }, 1000)
}
</script>

<style lang="less" scoped>
.block {
    height: 7vh;
}
</style>